﻿using System;
using System.Threading;
using System.Windows.Forms;
using PMIS.BLL;
using PMIS.BLL.Post;

namespace PMIS.UI.Post
{
	/// <summary>
	/// 岗位列表
	/// </summary>
	public partial class 岗位列表 : BaseDockForm
	{
		#region 私有变量
		/// <summary>
		/// 当前的查询参数
		/// </summary>
		private 岗位 CurrentQueryParamer = null;
		#endregion

		#region 单例
		/// <summary>
		/// 本类的实例
		/// </summary>
		private static 岗位列表 _form = null;
		/// <summary>
		/// 用于本类实现单例的Locker
		/// </summary>
		private static object locker = new object();
		/// <summary>
		/// 构造函数
		/// </summary>
		private 岗位列表()
		{
			InitializeComponent();
		}
		/// <summary>
		/// 获取本窗体的实例
		/// </summary>
		/// <returns></returns>
		public static 岗位列表 GetInstance()
		{
			if (_form == null || _form.IsDisposed)
			{
				lock (locker)
				{
					if (_form == null || _form.IsDisposed)
						_form = new 岗位列表();
				}
			}

			return _form;
		}
		#endregion

		#region 窗体事件
		/// <summary>
		/// 窗体载入事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void 岗位列表_Load(object sender, System.EventArgs e)
		{
			//	清空查询条件
			tsbtReset_Click(null, null);
			//	默认查询
			tsbtQuery_Click(null, null);
		} 
		#endregion

		#region 界面显示
		/// <summary>
		/// 更新列表显示
		/// </summary>
		private void UpdateListView()
		{
			try
			{
				if (this.InvokeRequired)
				{
					this.Invoke(new Action(UpdateListView));
				}
				else
				{
					toolStrip1.Enabled = dgvList.Enabled = wp.Enabled = false;

					//	分页参数
					PagingParameter pp = new PagingParameter();
					pp.CurrentPageIndex = wp.CurrentPageIndex;
					pp.PageSize = wp.PageSize;
					//pp.OrderBy = "岗位名称 Asc";

					//	查询并显示
					dgvList.AutoGenerateColumns = false;
					dgvList.DataSource = Singleton<BL_岗位>.GetInstance().Query(CurrentQueryParamer, ref pp);

					//	记录总数
					wp.TotalRecordCount = pp.TotalRecordCount;
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
			finally
			{
				toolStrip1.Enabled = dgvList.Enabled = wp.Enabled = true;
			}
		}
		#endregion

		#region 触发事件
		/// <summary>
		/// 翻页事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void wp_PageIndexChanged(object sender, EventArgs e)
		{
			tsbtRefresh_Click(null, null);
		}
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsbtQuery_Click(object sender, EventArgs e)
		{
			//	查询参数
			CurrentQueryParamer = new 岗位();
			//	岗位代码
			CurrentQueryParamer.岗位代码 = tbPostCode.Text.Trim();
			//	部门
			CurrentQueryParamer.部门班组ID = tbDepart.Text.Trim();

			//	岗位
			CurrentQueryParamer.岗位名称 = tbPostName.Text.Trim();
			//	是否一线岗位
			CurrentQueryParamer.是否一线岗位 = cbFirst.SelectedIndex < 1 ? string.Empty : cbFirst.Text;
			//	备注
			CurrentQueryParamer.备注 = tbRemark.Text.Trim();

			//	更新列表
			tsbtRefresh_Click(null, null);
		}
		/// <summary>
		/// 清空
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsbtReset_Click(object sender, EventArgs e)
		{
			tbPostCode.Text = string.Empty;
			tbDepart.Text = string.Empty;
			tbPostName.Text = string.Empty;
			cbFirst.SelectedIndex = 0;
			tbRemark.Text = string.Empty;
		}
		/// <summary>
		/// 新增
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsbtAdd_Click(object sender, EventArgs e)
		{
			try
			{
				岗位信息维护 form = 岗位信息维护.GetInstance("new");
				if (form == null)
					throw new Exception("获取岗位信息维护窗体时发生错误!");

				if (form.ShowDialog(this) == DialogResult.OK)
				{
					tsbtRefresh_Click(null, null);
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 修改
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsbtModify_Click(object sender, EventArgs e)
		{
			if (dgvList.SelectedRows.Count == 0)
			{
				ShowErrorMessage("请选择要修改的记录!");
				return;
			}

			try
			{
				string ID = dgvList.SelectedRows[0].Cells[ColumnID.Index].Value as string;
				if (string.IsNullOrEmpty(ID))
					return;

				岗位信息维护 form = 岗位信息维护.GetInstance(ID);
				if (form == null)
					throw new Exception("获取岗位信息维护窗体时发生错误!");

				if (form.ShowDialog(this) == DialogResult.OK)
				{
					tsbtRefresh_Click(null, null);
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsbtDelete_Click(object sender, EventArgs e)
		{
			if (dgvList.SelectedRows.Count == 0)
			{
				ShowErrorMessage("请选择要删除的记录!");
				return;
			}

			if (ShowYesNoMessage("确定删除该记录吗?") != DialogResult.Yes)
				return;

			try
			{
				string ID = dgvList.SelectedRows[0].Cells[ColumnID.Index].Value as string;
				if (string.IsNullOrEmpty(ID))
					return;

				if (Singleton<BL_岗位>.GetInstance().Delete(ID))
				{
					ShowSuccessMessage("记录删除成功!");
					//	更新列表
					tsbtRefresh_Click(null, null);
				}
				else
				{
					ShowErrorMessage("删除记录时发生错误!");
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 刷新
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsbtRefresh_Click(object sender, EventArgs e)
		{
			new Thread(new ThreadStart(UpdateListView)).Start();
			//new Action(UpdateListView).BeginInvoke(null, null);
		}
		/// <summary>
		/// 退出
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void tsbtExit_Click(object sender, EventArgs e)
		{
			this.Close();
		}
		/// <summary>
		/// 列表双击事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void dgvList_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			if (e.RowIndex < 0)
				return;

			try
			{
				string ID = dgvList.Rows[e.RowIndex].Cells[ColumnID.Index].Value as string;
				if (string.IsNullOrEmpty(ID))
					return;

				岗位信息维护 form = 岗位信息维护.GetInstance(ID);
				if (form == null)
					throw new Exception("无法创建岗位维护信息窗体");

				if (form.ShowDialog(this) == DialogResult.OK)
					tsbtRefresh_Click(null, null);
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		#endregion
	}
}